package com.atguigu.tingshu.album.task;

import cn.hutool.core.collection.CollUtil;
import com.atguigu.tingshu.album.mapper.AlbumInfoMapper;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.hash.BloomFilter;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component

public class RebuiltBloomFilterTask {
    @Autowired
    private AlbumInfoMapper albumInfoMapper;
    @Autowired
    private RedissonClient redissonClient;
    /**
     * 重建布隆过滤器 todo 后续改为xxl job
     * 每月1号凌晨两点执行
     */
    @Scheduled(cron = "0 0 3 * * ?")
//    @Scheduled(cron = "0/10 * * * * ?")
    public void rebuiltBloomFilter() {
        //处理扩容重建情况
        RBloomFilter<Long> oldBloomFilter = redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
        long expectedInsertions = oldBloomFilter.getExpectedInsertions();
        long count = oldBloomFilter.count();
        double falseProbability = oldBloomFilter.getFalseProbability();
        log.info("[专辑服务]触发了重建布隆过滤器的任务：现有期望数据规模{}，现有实际数据量{}", expectedInsertions,count);

        if (count >= expectedInsertions){
            log.info("[专辑服务]扩容布隆过滤器");
            //2.1 创建"新"的布隆过滤器完成初始化
            RBloomFilter<Long> newBloomFilter = redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER + ":new");
            newBloomFilter.tryInit(oldBloomFilter.getExpectedInsertions() * 2, oldBloomFilter.getFalseProbability());
            //2.2 将数据添加到"新"的布隆过滤器中
            this.addToBloomFilter(newBloomFilter);
            //2.2 删除"旧"的布隆过滤器
            oldBloomFilter.delete();
            //2.3 再对新的布隆进行重命名：改回旧的名称
            newBloomFilter.rename(RedisConstant.ALBUM_BLOOM_FILTER);
        }else {
            log.info("[专辑服务]重建布隆过滤器");
            //不满足扩容条件的话，直接重建布隆过滤器即可
            oldBloomFilter.delete();
            RBloomFilter<Long> newBloomFilter = redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
            newBloomFilter.tryInit(expectedInsertions, falseProbability);
            this.addToBloomFilter(newBloomFilter);
        }
    }

    /**
     * 将审核通过的专辑id添加到布隆过滤器中
     * @param bloomFilter
     */
    private void addToBloomFilter(RBloomFilter<Long> bloomFilter) {
        //根据审核条件查询专辑ID列表
        List<AlbumInfo> albumInfoList = albumInfoMapper.selectList(new LambdaQueryWrapper<AlbumInfo>()
                .eq(AlbumInfo::getStatus, "0301")
                .select(AlbumInfo::getId));

        //将专辑ID添加到布隆过滤器中
        if (CollUtil.isNotEmpty(albumInfoList)){
            albumInfoList.stream()
                    .map(AlbumInfo::getId)
                    .forEach(bloomFilter::add);
        }

    }

}
